var config = require('../../../config/config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: "wechat", value: "微信支付", checked: true },
      { name: "offline", value: "货到付款" },
    ],
     waterData:[],
     waterprice: 0,
     waterbill_category: [],
     goods_price: 0,
     gift_num: 0,
     lessprice: 0,
     buy_num: 0,
     total_num: 0,
     waterbill_id:0,
     bill_id:0,
     title: "",
     url: config.waterbill_pay,
     sid: 1,
     aid: 1,
  },

  radioChange: function (e) {
    var type = e.detail.value;
    if (type) {
      this.setData({
        url: config.waterbill_add,
      })
    }
  },

  //结算请求
  to_pay: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var mobile = parseInt(member_info.mobile)
    var that = this;
    if (!mobile) {
      wx.showModal({
        title: '提示',
        content: "你还未填写个人信息,请填写",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../personal/personal',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    var type = 0;
    if (that.data.url !== config.waterbill_pay) {
      type = 1;
    }
    wx.request({
      url: this.data.url,
      method: "POST",
      data: {
        type: type,
        bill_id: this.data.bill_id,
        waterbill_id: this.data.waterbill_id,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
        openid: member_info.openid
      },
      success: function (res) {
        var ispaysuccess = 0;
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          if (that.data.url == config.waterbill_pay) {
            that.Payment(res.data.data)
          } else {
            var ispaysuccess = 1;
            wx.redirectTo({
              url: '../pay-success/pay-success?ispaysuccess=' + ispaysuccess,
            })
          }
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  //微信支付
  Payment: function (data) {
    var ispaysuccess = 0;
    wx.requestPayment({
      'timeStamp': String(data.timeStamp),
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        var ispaysuccess = 1;
        wx.redirectTo({
          url: '../pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      },
      'fail': function (res) {
        // wx.showToast({ title: '支付失败', icon: 'loading', duration: 20000 });
        var ispaysuccess = 0;
        wx.redirectTo({
          url: '../pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.setData({
     waterData: options,
     waterprice: options.waterprice,
     title: options.title,
     total_num: options.total_num,
     waterbill_id: options.waterbill_id,
     bill_id: options.bill_id
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})